package com.example.day_0813_103;

import com.example.day_0813_103.slice.Six_AbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

public class Six_Ability extends Ability implements IAbilityContinuation {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(Six_AbilitySlice.class.getName());
        // 权限申请
        if(verifySelfPermission(SystemPermission.DISTRIBUTED_DATASYNC) != IBundleManager.PERMISSION_GRANTED) {
            // 如果可以用户可以申请 (DISTRIBUTED_DATASYNC) 权限
            if (canRequestPermission("ohos.permission.DISTRIBUTED_DATASYNC")) {
            // 那么申请 (DISTRIBUTED_DATASYNC)权限 ，返回码 1000
            requestPermissionsFromUser(new String[] {SystemPermission.DISTRIBUTED_DATASYNC},0);
            }
        }
    }
    //    申请权限的回调
    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        // 当 返回的权限为空 或者等于0 或者 授权数组为空 或者等于0  则授权失败
        if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) {
            return;
        }
        // 当返回码 等于 请求码 1000
        if (requestCode == 0) {
            //  当数组授权 0号索引 为 IBundleManager.PERMISSION_GRANTED 代表授权成功
            if (grantResults[0] == IBundleManager.PERMISSION_DENIED) {
                // 关闭页面
//                terminateAbility();
            }
        }
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {
        terminateAbility();
    }
}
